package com.guigu.xm.lq.controller;

import com.guigu.xm.advice.Interceptor;
import com.guigu.xm.lq.service.impl.SysAuthorizationRoleServiceImpl;
import com.guigu.xm.pojo.SysAuthorizationRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class qunaXianIntertor implements HandlerInterceptor {
    @Autowired(required = false)
    SysAuthorizationRoleServiceImpl sars;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Interceptor annotation = ((HandlerMethod) handler).getMethod().getAnnotation(Interceptor.class);
        String login_id=  (String)request.getSession().getAttribute("login_id");
        if(annotation!=null){
            SysAuthorizationRole sysAuthorizationRole = sars.selectAuth(annotation.methodName(), login_id);
            System.out.println(".."+sysAuthorizationRole+".."+login_id+",,,,"+annotation.methodName());
            if(sysAuthorizationRole!=null){
                return true;
            }else{
                System.out.println("拦截到了");
                return false;
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
